<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no">
    <meta http-equiv="X-UA-Compatible" content="ie=edge,chrome=1">
    <meta name="referrer" content="never">
    <title>多样式动画封装</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        ul {
            list-style: none;
        }

        .box{
            position: absolute;
            top: 50px;
            left: 50px;
            width: 150px;
            height: 150px;
            background-color: #f00;
        }
    </style>
</head>
<body>
    <div class="box"></div>
    <script type="text/javascript">
        var box = document.querySelector(".box");

        // width => 600  height => 600  opacity => .3
        box.onclick = function (){
            // 不定参函数 obj  key: value
            animate(this, {
                width: 400,
                height: 600,
                opacity: .3
            });
        }

        function animate(ele, params){
            // params => { width: 600, height: 600, opacity: .3 ...}
            // 判断是否用定时器，有清除
            if(ele.timer){
                clearInterval(ele.timer);
            }
            ele.timer = setInterval(function (){
                var start, end;
                // 所有样式都到达终点表示 true，假设都到达了终点
                var flag = true;
                console.log("=================");
                for(var i in params){
                    // console.log("样式名：", i, "样式值", params[i]);
                    // 透明度 -- 起点，终点放大，赋值时缩小
                    if(i == "opacity"){
                        start = parseInt(getComputedStyle(ele, null)[i] * 100);
                        end = params[i] * 100;
                    }else {
                        start = parseInt(getComputedStyle(ele, null)[i]);
                        end = params[i];
                    }

                    // 设置步长
                    var step = (end - start)/10;
                    if(Math.abs(step) < 1){
                        step = step > 0 ? 1 : Math.floor(step);
                    }

                    // 赋值
                    if(i == "opacity"){
                        ele.style.opacity = (start + step)/100;
                    }else {
                        ele.style[i] = start + step + "px";
                    }

                    if(start + step !== end){
                        // 有一个没有到达终点，整体状态改为false
                        flag = false;
                        console.log("没有到达终点的属性", i);
                    }
                }

                // 假设成真
                if(flag){
                    clearInterval(ele.timer);
                    ele.timer = null;
                }
               
            }, 1000/60)
        }
    </script>
</body>
</html>